﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using _4T.DAL;
using _4T.Data;
using _4T.BLL;

namespace _4T
{
    /// <summary>
    /// Summary description for ProfileService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ProfileService : System.Web.Services.WebService
    {
        /// <summary>
        /// Get profile list with filter column, personal code, stage and status.
        /// </summary>
        /// <param name="field"></param>
        /// <param name="personalCode"></param>
        /// <param name="stage"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [WebMethod(Description = "Get profile list with personal code, stage and status.")]
        public List<ProfileInfo> GetProfileList(string column, string personalCode, int stage, int status)
        {
            ProfileInfoBLL profileBLL = new ProfileInfoBLL();
            return profileBLL.GetProfileList(column, personalCode, stage, status);
        }

        /// <summary>
        /// Insert information of a profile.
        /// </summary>
        /// <param name="profile"></param>
        /// <returns>true if succes, false if fail</returns>
        [WebMethod(Description = "Insert information of a profile.")]
        public bool InsertProfileInfo(Profile profile)
        {
            ProfileBLL profileBLL = new ProfileBLL();
            return profileBLL.InsertProfileInfoWithMedia(profile, string.Empty, string.Empty, string.Empty, profile.InfoSupplierInfo.PersonalCode, DateTime.Now, string.Empty);
        }

        [WebMethod(Description = "Insert Device ID.")]
        public AddDeviceIDReturn InsertDeviceID(string device_id, int device_type, string personal_code)
        {
            AddDeviceIDReturn mReturn = new AddDeviceIDReturn();
            NotificationDAL nofiticationDAL = new NotificationDAL();
            int result = 0;
            string message = string.Empty;
            try
            {
                result = nofiticationDAL.InsertDeviceID(device_id, device_type, personal_code);
            }
            catch (Exception ex)
            {
                message = ex.ToString();
            }

            if (result > 0)
            {
                mReturn.Result = true;
            }
            else
            {
                mReturn.Result = false;
            }

            mReturn.ErrorDescription = message;
            return mReturn;
        }

        /// <summary>
        /// Validate user.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>personal info if success, null if fail</returns>
        [WebMethod(Description = "Validate user.")]
        public PersonalInfo ValidateUser(string username, string password)
        {
            PersonalInfoBLL personBLL = new PersonalInfoBLL();
            return personBLL.ValidatePerson(username, password);
        }

        /// <summary>
        /// Get patient info by profile code.
        /// </summary>
        /// <param name="profileCode"></param>
        /// <returns></returns>
        [WebMethod(Description = "Get patient info by profile code.")]
        public PatientInfo GetPatientInfoByProfileCode(string profileCode)
        {
            PatientInfoBLL patientBLL = new PatientInfoBLL();
            return patientBLL.GetPatientInfo(profileCode);
        }

        /// <summary>
        /// Get medical info of patient by profile code.
        /// </summary>
        /// <param name="profileCode"></param>
        /// <returns></returns>
        [WebMethod(Description = "Get medical info of patient by profile code.")]
        public MedicalInfo GetMedicalInfoByProfileCode(string profileCode)
        {
            MedicalInfoBLL medicalBLL = new MedicalInfoBLL();
            return medicalBLL.GetMedicalInfo(profileCode);
        }

        /// <summary>
        /// Get info of supplier by profile code.
        /// </summary>
        /// <param name="profileCode"></param>
        /// <returns></returns>
        [WebMethod(Description = "Get info of supplier by profile code.")]
        public InfoSupplierInfo GetInfoSupplierInfoByProfileCode(string profileCode)
        {
            InfoSupplierInfoBLL supplierBLL = new InfoSupplierInfoBLL();
            return supplierBLL.GetInfoSupplierInfo(profileCode);
        }

        /// <summary>
        /// Update status of profile.
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        [WebMethod(Description = "Update status of profile.")]
        public bool UpdateProfileStatus(ProfileStatus status)
        {
            ProfileStatusBLL statusBLL = new ProfileStatusBLL();
            return statusBLL.UpdateProfileStatusFromApp(status);
        }

        /// <summary>
        /// Get list of personal info in specific role.
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [WebMethod(Description = "Get list of personal info in specific role.")]
        public List<PersonalInfo> GetPersonalListInRole(int role)
        {
            PersonalInfoBLL personBLL = new PersonalInfoBLL();
            return personBLL.GetPersonalListInRole(role);
        }

        /// <summary>
        /// Get total number of profile.
        /// </summary>
        /// <returns></returns>
        [WebMethod(Description = "Get total number of profile.")]
        public int GetTotalNumberOfProfile()
        {
            ProfileInfoBLL profileInfoBLL = new ProfileInfoBLL();
            return profileInfoBLL.GetTotalNumberProfile(0);
        }

        /// <summary>
        /// Get list profile with paging.
        /// </summary>
        /// <param name="page">Number of page</param>
        /// <param name="pageSize">Size of page</param>
        /// <returns></returns>
        [WebMethod(Description = "Get list profile with paging.")]
        public List<ProfileInfoStatusDetail> GetListProfileWithPaging(int page, int pageSize)
        {
            ProfileBLL profileBLL = new ProfileBLL();
            return profileBLL.GetListProfilePaging(page, pageSize, "", 0);
        }

        /// <summary>
        /// Get link and description of a stage of profile.
        /// </summary>
        /// <param name="profileCode">Code of profile</param>
        /// <param name="stage">Stage of profile</param>
        /// <returns></returns>
        [WebMethod(Description = "Get link and description of a stage of profile.")]
        public ProfileStageLink GetProfileStageLinkByProfileCodeAndStage(string profileCode, int stage)
        {
            ProfileStageLinkBLL stageBLL = new ProfileStageLinkBLL();
            return stageBLL.GetProfileStageLinkByProfileCodeAndStage(profileCode, stage);
        }

        /// <summary>
        /// Update status of profile with return is string
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        [WebMethod(Description = "Update status of profile with return is string.")]
        public string UpdateProfileStatusWithErrorCodeReturn(ProfileStatus status)
        {
            try
            {
                ProfileStatusBLL statusBLL = new ProfileStatusBLL();
                return statusBLL.UpdateProfileStatusWithErrorCodeReturn(status);
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }

        [WebMethod(Description = "Insert information of a profile with result is string.")]
        public string InsertProfileInfoWithErrorCode(Profile profile)
        {
            /*ProfileBLL profileBLL = new ProfileBLL();
            return profileBLL.InsertProfileInfoWithErrorCode(profile, string.Empty);*/

            ProfileBLL profileBLL = new ProfileBLL();
            return profileBLL.InsertProfileInfoWithErrorCode(profile, string.Empty, string.Empty, string.Empty, profile.InfoSupplierInfo.PersonalCode, DateTime.Now, string.Empty);
        }

        [WebMethod(Description = "Get list comments.")]
        public List<Comment> GetListComment(string ProfileCode, int Stage)
        {
            CommentDAL commentDAL = new CommentDAL();
            return commentDAL.GetListComment(ProfileCode, Stage);
        }

        [WebMethod(Description = "Insert comments.")]
        public int InsertComment(string PersonalCode, string ProfileCode, int Stage, string body)
        {
            CommentDAL commentDAL = new CommentDAL();
            return commentDAL.InsertComment(PersonalCode, ProfileCode, Stage, body);
        }
    }
}
